package com.atguigu.gmall.realtime.app;

import com.atguigu.gmall.realtime.common.Constant;
import com.atguigu.gmall.realtime.util.SQLUtil;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lzc
 * @Date 2022/12/26 15:03
 */
public abstract class BaseSQLApp {
    public abstract void handle(StreamExecutionEnvironment env,
                                StreamTableEnvironment tEnv);
    
    public void init(int port,
                     int p,
                     String ckAndAndJobName) {
        System.setProperty("HADOOP_USER_NAME", "atguigu");
        // 1. 创建流的执行环境
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", port);  // 如果不设置, 则会使用一个随机的 web 端口
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(p); // 一般和 kafka 的分区数保持一致
        // 设置状态后端和 checkpoint
        env.setStateBackend(new HashMapStateBackend());// 1. HashMap状态后端  2. rocksdb 状态后端
        env.enableCheckpointing(3000); // 设置 checkpoint 的周期: 生成环境一般是分钟级别
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
        env.getCheckpointConfig().setMaxConcurrentCheckpoints(1); // 设置同时进行的 checkpoint 的数量
        // 配置这个, 上面这个可以不设置
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(500);// 上一个 checkpoint 结束 500ms 之后,才会开启下一个
        env.getCheckpointConfig().setCheckpointTimeout(60 * 1000);  // checkpoint 的超时时间
        env.getCheckpointConfig().setExternalizedCheckpointCleanup(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/gmall/" + ckAndAndJobName);
        
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        
        // 通过 tEnv 来设置sql应用的名字
        tEnv.getConfig().getConfiguration().setString("pipeline.name", ckAndAndJobName);
        
        
        handle(env, tEnv);
        
        
    }
    
    public void readOdsDb(StreamTableEnvironment tEnv, String groupId) {
        tEnv.executeSql("create table ods_db(" +
                            "  `database` string, " +
                            "  `table` string, " +
                            "  `type` string, " +
                            "  `data` map<string, string>, " +
                            "  `old` map<string, string>, " +
                            "  `ts` bigint," +
                            "  `pt` as proctime()" + // 添加处理时间, 主要是 lookup join 使用
                            ")" + SQLUtil.getKafkaSourceDDL(Constant.TOPIC_ODS_DB, groupId));
        
    }
    
    public void readBaseDic(StreamTableEnvironment tEnv) {
        tEnv.executeSql("create table base_dic (" +
                            "  dic_code string," +
                            "  dic_name string " +
                            ") WITH (" +
                            "  'connector' = 'jdbc'," +
                            "  'url' = 'jdbc:mysql://hadoop162:3306/gmall2022?useSSL=false'," +
                            "  'table-name' = 'base_dic', " +
                            "  'username' = 'root', " +
                            // 对查到的维度数据, 缓存到内存中的时间
                            // 要在准确性和效率之间达到一个平衡
                            "  'lookup.cache.ttl' = '2 hour', " +
                            // 对查到的维度数据, 最多缓存 100 条
                            "  'lookup.cache.max-rows' = '100', " +
                            "  'password' = 'aaaaaa' " +
                            ")");
        
    }
}
